<template>
  <div>
  <p>评估任务号为:{{this.ruleForm.evaluatetaskid}}</p>
  <el-form :model="ruleForm" :rules="rules" :inline="true" ref="ruleForm" label-width="180px" class="demo-ruleForm">
    <el-form-item label="未选择学校">
      <el-checkbox-group v-model="checkedschool">
        <el-checkbox
            v-for="item in checkboxoptions"
            :label="item.label"
            :key="item.label"
        >
        </el-checkbox>
      </el-checkbox-group>
    </el-form-item>
      <el-form-item label="已选择学校">
        <el-checkbox-group v-model="checkedschool">
          <el-checkbox
              v-for="item in selectedcheckboxoptions"
              :label="item.label"
              :key="item.label"
          >
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
<!--    <el-form-item>-->
<!--      <el-button type="primary" @click="trans">测试</el-button>-->
<!--      <el-button type="primary" @click="submitForm('ruleForm')">确认修改</el-button>-->
<!--    </el-form-item>-->
  </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      checkboxlen:'',
      checkedschool:[],
      checkboxoptions:[],
      selectedcheckboxoptions:[],
      ruleForm: {
        evaluatetaskid:this.$route.query.evaluatetaskid,
        schoolname: ''
      },
      rules: {
        evaluatetaskid: [
          { required: true, message: '请填写评估任务号', trigger: 'blur' }
        ],
        schoolname: [
          { required: true, message: '请填写学校名称', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    trans(){
      let code=0
      let sn=""
      let ob={}
      let checklen=this.checkedschool.length
      while(checklen){
        checklen--
        console.log(this.ruleForm.schoolname)
        console.log(this.ruleForm)
        console.log(checklen)
      }
    },
    //专家发布任务给学校 管理端获取任务号和学校名称
    submitForm(formName) {
      const _this=this
      let sign=0;
      let sn=""
      let checklen=this.checkedschool.length
      this.$refs[formName].validate((valid) => {
        if (valid) {
            // checklen=this.checkedschool.length
            while (checklen){
              checklen--;
              sn=this.checkedschool[checklen]
              this.ruleForm={evaluatetaskid: this.ruleForm.evaluatetaskid,schoolname: sn}
              console.log(this.ruleForm)
              axios.post("http://localhost:8081/evaluatetaskarrange/save/",this.ruleForm).then(function (res){
                if (res.data.msg==="发布成功"&&sign===0){
                  sign=1
                  _this.$message({
                    message: '发布成功！',
                    type: 'success'
                  });
                  // 添加成功后页面跳转
                  _this.$router.push({
                    path:'/EvaluateTaskListForExpert',
                    query:{
                        selectedschoolnumber:this.selectedcheckboxoptions.length
                    }
                  })
                }
                else if (sign===0&&res.data.msg!=="发布成功"){
                  sign=1
                  console.log(sign)
                  _this.$message({
                    message: '评估号不存在！请重新输入',
                    type: 'fail'
                  });
                }
              })
            }
            // this.ruleForm.schoolname=this.checkedschool[0]
          // axios.post("http://localhost:8081/evaluatetaskarrange/save/",this.ruleForm).then(function (res){
          //   if (res.data.msg==="发布成功"){
          //     _this.$message({
          //       message: '发布成功！',
          //       type: 'success'
          //     });
          //     // 添加成功后页面跳转
          //     _this.$router.push('/EvaluateTaskListForExpert')
          //   }
          //   else{
          //     _this.$message({
          //       message: '评估号不存在！请重新输入',
          //       type: 'fail'
          //     });
          //   }
          // })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    const a=localStorage.getItem("access-expert")
    if(!a){
      this.$alert('未登录','提示',{
        confirmButtonText:'确定'
      }).then((res)=>{
        this.$router.replace({path:'/login'})
      })
    }
    else {
      let len=0
      // axios.get("http://localhost:8081/evaluatetask/selectone/"+this.$route.query.id).then((res)=>{
      //   this.ruleForm.evaluatetaskid=res.data.evaluatetaskid
      // })
      axios.get("http://localhost:8081/school/findAll").then((res)=>{
            len=res.data.length;
            this.checkboxlen=res.data.length
            while(len){
              len--;
              this.checkboxoptions.push({label:res.data[len].schoolname})
            }
      })
      axios.get("http://localhost:8081/evaluatetaskarrange/search/11/"+this.$route.query.evaluatetaskid).then((res)=>{
        len=res.data.length;
        console.log(res.data)
        this.checkboxlen=res.data.length
        while(len){
          len--;
          this.selectedcheckboxoptions.push({label:res.data[len].schoolname})
        }
      })
    }
  }



}
</script>